package cn.qu.util;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @Description: 彩色打印
 * @Author XiaoLong.Qu
 * @DateTime 2021年8月8日 下午11:54:53
 */
public class PrintUtils {

	public static void main(String[] args) {
		Style[] styles = Style.values();
		for (Style style : styles) {
			Color[] colors = Color.values();
			for (Color color : colors) {
				print("什么颜色", color, style);
				print(" ", color);
			}
		}
	}

	final static PrintStream writer = System.out;
	final static List<Map<String, String[]>> list = new ArrayList<>();
	static {
		for (int x = 1; x <= 10; x++) {
			Map<String, String[]> map = new HashMap<>();
			int c1 = 90;
			int c2 = 30;

			for (int i = 0; i < 10; i++) {
				map.put(c1 + i + "", new String[] { "\033[" + (c1 + i) + ";" + x + "m", "\033[0m" });
				map.put(c2 + i + "", new String[] { "\033[" + (c2 + i) + ";" + x + "m", "\033[0m" });
			}

			list.add(map);
		}
	}

	public static void print(String msg, Color color) {
		print(msg, color, Style.BOLD);
	}

	public static void print(String msg, Color color, Style style) {
		String[] strings = list.get(style.value()).get(color.value() + "");
		writer.print(strings[0] + msg + strings[1]);
	}

	public static void println(String msg, Color color) {
		println(msg, color, Style.BOLD);
	}

	public static void println(String msg, Color color, Style style) {
		println(msg, color, style, 170);
	}

	public static void println(String msg, Color color, Style style, int widthLength) {
		List<String> msgList = StringUtils.split(new StringBuilder(msg), widthLength);
		String[] strings = list.get(style.value()).get(color.value() + "");
		msgList.forEach(s -> writer.println(strings[0] + s + strings[1]));
	}

	public static enum Style {
		BOLD(0), NON(1), ITALICS(2), UNDERLINE(3), BACKGROUND(6), TRANSPARENT(7), LINE_THROUGH(8);

		private int style;

		Style(int style) {
			this.style = style;
		}

		public int value() {
			return style;
		}
	}

	public static enum Color {
		BLACK(30), RED(31), GREEN(32), YELLOW(33), BLUE(34), PURPLE(35), CYAN_BLUE(36), SILVERY_WHITE(37),
		LIGHT_BLACK(90), LIGHT_RED(91), LIGHT_GREEN(92), LIGHT_YELLOW(93), LIGHT_BLUE(94), LIGHT_PURPLE(95),
		CYAN_GREEN(96), WHITE(97);

		private int color;

		Color(int color) {
			this.color = color;
		}

		public int value() {
			return color;
		}
	}

}
